import api from "../../api/sop";
const { err, toast, to } = require("../../utils/wxapi");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    amount: "",
    user: wx.getStorageSync("user"),
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.user();
  },
  async pay() {
    try {
      const { amount, user } = this.data;
      if (amount > 200) return toast("单次提现金额不得超过200元");
      if (amount > user.amount) return toast("大于可提现的金额");
      if (!amount) return toast("请输入提现金额");
      const res = await api.money({ amount, userId: user.userId });
      if (res.respCode === 200) {
        toast("提现成功");
      } else {
        toast(res.msg);
        err(res.msg);
      }
    } catch (error) {
      err(error);
    }
  },
  async user() {
    try {
      const res = await api.getUser();
      if (res.respCode === 200) {
        const user = JSON.parse(res.source);
        wx.setStorageSync("user", user);
        this.setData({ user });
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  go() {
    to("/two/txlist/txlist");
  },
});
